package com.exam.exception;

import com.exam.Result.ApiResult;
import com.exam.Result.ApiResultHandler;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

/**
 * @Author 戴志豪
 * @date 2024/4/12 23:15
 */
@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public ApiResult handleSQLIntegrityConstraintViolationException(SQLException e) {
        return ApiResultHandler.buildApiResult(1,"数据库异常，请联系管理员",null); // 返回 SQLIntegrityConstraintViolationException 的消息
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResult handleException(Exception e) {
        return ApiResultHandler.buildApiResult(1,e.getMessage(),null); // 返回异常的消息
    }
}
